<?php 
namespace App\Console\Commands;

include base_path() . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\Link;
use EasyWeChat\Message\News;
use Illuminate\Console\Command;
use Log;
use DB;
use App\WxPublicUser;
use App\WxMessage;
use App\WxMsglog;

class SendMsg extends Command {

    protected $config = [
        'app_id' => 'wx555be570d21859aa',
        'secret' => 'f92678be7687d1e1b1471ca9c066f242',
        'token' => 'leyougame2018'
    ];

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'send:msg';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app = new Application($this->config);
        // $now = time() - (48 * 3600);
        // $ago_time = date('Y-m-d H:i:s', $now);
        $users = WxPublicUser::get();//全部公众号用户
        if ($users) {
            foreach ($users as $user) {
                // $info = $app->user->get($user->openid);
                // if ($info->subscribe != 1) {//已经不再关注公众号的用户
                //     continue;
                // }                
                $infos = WxMessage::where(['status' => 1, 'type' => 1])->where('begin_time', '<', date('H.i', time()))->where('end_time', '>', date('H.i', time()))->get();//获取公众平台循环推送信息
                if ($infos) {
                    foreach ($infos as $info) {
                        $log = WxMsglog::where(['user_id' => $user->id, 'msg_id' => $info->id, 'type' => 1])->whereBetween('created_at', [date("Y-m-d"), date("Y-m-d",strtotime("+1 day"))])->orderBy('created_at', 'DESC')->first();
                        if (!$log) {
                            $wxinfo = base64_decode($info->content);
                            /*发送信息*/
                            $text = new Text(['content' => $wxinfo]);
                            $openId = $user->openid;
                            try {
                                $staff = $app->staff;
                                $staff->message($text)->to($openId)->send();
                                $wxlog = new WxMsglog;
                                $wxlog->user_id = $user->id;
                                $wxlog->msg_id = $info->id;
                                $wxlog->type = $info->type;
                                $wxlog->save();
                            } catch (\EasyWeChat\Core\Exception $e) {
                                continue;
                            }
                        } else {
                            if ((time() - strtotime($log->created_at) > $info->interval * 60)) {//超过间隔的进行推送
                                $wxinfo = base64_decode($info->content);
                                /*发送信息*/
                                $text = new Text(['content' => $wxinfo]);
                                $openId = $user->openid;
                                try {
                                    $staff = $app->staff;
                                    $staff->message($text)->to($openId)->send();
                                    $wxlog = new WxMsglog;
                                    $wxlog->user_id = $user->id;
                                    $wxlog->msg_id = $info->id;
                                    $wxlog->type = $info->type;
                                    $wxlog->save();
                                } catch (\EasyWeChat\Core\Exception $e) {
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}